<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Primitive user CRUD model class 
 */
Class User extends CI_Model {

    public $users_table = "users";

    /**
     * Checks login validity
     * 
     * @param type $username
     * @param type $password
     * @return type 
     */
    function login($username, $password) {
        $this->db->select('id, username, password');
        $this->db->from($this->users_table);
        $this->db->where('username = ' . "'" . $username . "'");
        $this->db->where('password = ' . "'" . MD5($password) . "'");
        $this->db->limit(1);

        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return $query->result();
        } else {
            return false;
        }
    }

    /**
     * Add new user
     *
     * @param type $username
     * @param type $password
     * @param type $email
     * @return type 
     */
    function add($username, $password, $email) {
        // configure data to insert
        $data = array(
            'username' => $username,
            'password' => MD5($password),
            'email' => $email
        );

        // insert data
        $this->db->insert($this->users_table, $data);

        // done
        return true;
    }

    /**
     *
     * @param type $id
     * @return array (user_id, username, user)
     */
    function retrieve($id) {
        if (!isset($id) || $id < 0)
            throw new Exception("user id have to be specified!");

        // constructing active record query
        $query = $this->db->get_where($this->users_table, array('id' => $id));
        // do the query   
        return $query->result();
    }

    /**
     * Update user by id.
     * @param type $id
     * @param type $username
     * @param type $password
     * @return true on success
     */
    function update($id, $username, $password, $email) {
        // configure data to update
        $data = array(
            'username' => $username,
            'password' => $password,
            'email' => $email
        );

        // do selection and update data
        $this->db->where('id', $id);
        $this->db->update($this->users_table, $data);

        return true;
    }

    /**
     * Delete user
     * @param type $id
     * @return true on success
     */
    function delete($id) {
        $this->db->where('id', $id);
        $this->db->delete($this->users_table);
        return true;
    }

    /**
     * returns the users 
     * @param type $limit the number of returned users
     * @param type $offset from this number.
     * @return type 
     */
    function get_users($limit = NULL, $offset = NULL) {
        $this->db->limit($limit, $offset);
        return $this->db->get($this->users_table);
    }

    /**
     * Returns the number of users
     * @return type 
     */
    function count_users() {
        return $this->db->count_all_results($this->users_table);
    }

    /**
     * Returns the id of the user, or false if no such user
     * @param type $username 
     */
    function get_user_id($username) {
        $query = $this->db->query("SELECT `id` FROM $this->users_table WHERE `username` = '$username' LIMIT 1");
        if ($query->num_rows() == 1) {
            $row = $query->row();
            return $row->id;
        } else {
            return false;
        }
    }

}

